* Figure 1 - H, R and health shocks over time
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if AGE1<65 & AGE1>24
drop if year<2000 | year>2013
keep if INSCOP_Y1==1  // this covers R1-R3 which we are using here.

gen year_sq=year^2

replace H1=0 if H1==3
replace H1=1 if H1==2 // FAIR OR POOR

label var R3 "R"
rename R3 R

collapse (mean) H1 R dp du s AGE1 , by(educ_group year) 


#delimit ;
line dp year if educ_group==1, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line dp year if educ_group==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line  dp year if educ_group==3, lwidth(medthick) lcolor(navy)
  ||, 
  ylabel(0.0(.10).30)
  xlabel(2000(1)2013)
  ytitle("Fraction")
xtitle("Age")
   title("Fraction with dp", color(black))
  legend(label(1 "<=HS") label(2 "Some College") label(3 "College")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\dp_by_yrs.eps", replace



#delimit ;
line du year if educ_group==1, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line du year if educ_group==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line  du year if educ_group==3, lwidth(medthick) lcolor(navy)
  ||, 
  ylabel(0.1(.10).40)
  xlabel(2000(1)2013)
  ytitle("Fraction")
xtitle("Age")
   title("Fraction with du", color(black))
  legend(label(1 "<=HS") label(2 "Some College") label(3 "College")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\du_by_yrs.eps", replace



#delimit ;
line s year if educ_group==1, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line s year if educ_group==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line  s year if educ_group==3, lwidth(medthick) lcolor(navy)
  ||, 
  ylabel(0.3(.10).60)
  xlabel(2000(1)2013)
  ytitle("Fraction")
xtitle("Age")
   title("Fraction with s", color(black))
  legend(label(1 "<=HS") label(2 "Some College") label(3 "College")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\s_by_yrs.eps", replace



#delimit ;
line R year if educ_group==1, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line R year if educ_group==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line  R year if educ_group==3, lwidth(medthick) lcolor(navy)
  ||, 
  ylabel(0.0(.10).20)
  xlabel(2000(1)2013)
  ytitle("Fraction")
xtitle("Age")
   title("Fraction with High R", color(black))
  legend(label(1 "<=HS") label(2 "Some College") label(3 "College")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\R_by_yrs.eps", replace


#delimit ;
line H1 year if educ_group==1, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line H1 year if educ_group==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line  H1 year if educ_group==3, lwidth(medthick) lcolor(navy)
  ||, 
  ylabel(0.0(.10).60)
  xlabel(2000(1)2013)
  ytitle("Fraction")
xtitle("Age")
   title("Fraction in Poor or Fair H", color(black))
  legend(label(1 "<=HS") label(2 "Some College") label(3 "College")  )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\H_by_yrs.eps", replace
}


* TABLES 60 AND 61
* % with ESHI by H and education
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if INSCOP_Y1==1
keep if AGE1<65

* all men 25-64
table H1 educ_group [aw=Sample_Weight], statistic(mean PRIVATE_HI_Y1) nototal nformat(%5.2f)
collect title "Fraction with ESHI, ages 25-64, MEPS"
collect export "${out_tables}/Insurance1.tex", tableonly replace

* workers only
table H1 educ_group if labor_force_Y1!=1 [aw=Sample_Weight], statistic(mean PRIVATE_HI_Y1) nototal nformat(%5.2f)
collect title "Fraction with ESHI, Workers Only, ages 25-64, MEPS"
collect export "${out_tables}/Insurance2.tex", tableonly replace
}


* Save data for Figure 6
* H transitions - we save these transitions and then plot them together with the ones from the model  
qui{ 
clear
use "${data}/MEPS_merged_Health_Whites.dta"
drop if year==2013
keep if INSCOP_Y2==1 // this covers R3-R5 which we are using here.

// from good to bad
gen H_trans=0 if (H1==3 ) & H3==3 // stay in good
replace H_trans=1 if H1==3 & (H3==2 | H3==1)  // transition to worse health

// from bad to good
gen H_trans1=0 if  (H1==2 | H1==1) &  (H3==2 | H3==1)
replace H_trans1=1 if (H1==2 | H1==1) &  H3==3 

// from poor to better
gen H_trans2=0 if  ( H1==1) &  ( H3==1)
replace H_trans2=1 if (H1==1) &  (H3==3 | H3==2)

// from poor to poor
gen H_trans3=0 if  ( H1==1) &  ( H3!=1 & H3!=.)
replace H_trans3=1 if (H1==1) &  (H3==1)

// from fair to fair
gen H_trans4=0 if  ( H1==2) &  ( H3!=2 & H3!=.)
replace H_trans4=1 if (H1==2) &  (H3==2)

// from good to good
gen H_trans5=0 if  ( H1==3) &  ( H3!=3 & H3!=.)
replace H_trans5=1 if (H1==3) &  (H3==3)

// from fair/good to poor
gen H_trans6=0 if  ( H1==3 | H1==2) &  ( H3==1)
replace H_trans6=1 if ( H1==3 | H1==2) &  ( H3!=1 & H3!=.)


drop if H1==. | H3==.
keep if AGE_YR1>=25 & AGE_YR1<75

rename AGE_YR1 age

* age groups
gen age_group=27  if age>=25 & age<30
replace age_group=32  if age>=30 & age<35
replace age_group=37  if age>=35 & age<40
replace age_group=42  if age>=40 & age<45
replace age_group=47  if age>=45 & age<50
replace age_group=52  if age>=50 & age<55
replace age_group=57  if age>=55 & age<60
replace age_group=62  if age>=60 & age<65
replace age_group=67  if age>=65 & age<70
replace age_group=72  if age>=70 & age<75

collapse (mean) GB=H_trans BG=H_trans1  PG=H_trans2  PP=H_trans3  FF=H_trans4  GG=H_trans5 FGP=H_trans6, by(educ_group age_group) 
reshape wide GB BG PG PP FF GG FGP, i(age_group) j(educ_group)
rename age_group age
sort age
save "${data}/Health Trans Moments data.dta",  replace
}


* Save data for Figure 5
* Transitions for people with d shocks
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
drop if year==2013
keep if INSCOP_Y2==1 // this covers R3-R5 which we are using here.

// from good to bad
gen H_trans=1 if (H1==3 ) & H3==3 // stay in good
replace H_trans=0 if H1==3 & (H3==2 | H3==1)  // transition to worse health

gen d_shock=1 if dp==1 | du==1
replace d_shock=0 if dp==0 & du==0

drop if H1==. | H3==.
keep if AGE_YR1>=25 & AGE_YR1<75

rename AGE_YR1 age

* age groups
gen age_group=27  if age>=25 & age<30
replace age_group=32  if age>=30 & age<35
replace age_group=37  if age>=35 & age<40
replace age_group=42  if age>=40 & age<45
replace age_group=47  if age>=45 & age<50
replace age_group=52  if age>=50 & age<55
replace age_group=57  if age>=55 & age<60
replace age_group=62  if age>=60 & age<65
replace age_group=67  if age>=65 & age<70
replace age_group=72  if age>=70 & age<75

collapse (mean) GG=H_trans, by(educ_group age_group d_shock) 
rename age_group age
sort educ_group age d_shock
save "${data}/Health Trans Moments data shocks.dta",  replace
}


* Table 47 - top panel
* correlation between H and income 
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if INSCOP_Y1==1
keep if AGE_YR1<65
rename TOTALINC_Y1 income
rename AGE_YR1 age
gen age_sq=age^2
gen age_cub=age^3
rename educ_group education
rename H1 H
replace H=2 if H==1
 
 
reg income i.H age age_sq age_cub if education==1 //[pw=Sample_Weight]
eststo
reg income i.H age age_sq age_cub if education==2 //[pw=Sample_Weight]
eststo
reg income i.H age age_sq age_cub if education==3 //[pw=Sample_Weight]
eststo
reg income  i.H age age_sq age_cub if EMPLOYMENT_YR1_v1  == 1  & education==1 //[pw=Sample_Weight]
eststo
reg income  i.H age age_sq age_cub if EMPLOYMENT_YR1_v1  == 1  & education==2 //[pw=Sample_Weight]
eststo
reg income  i.H age age_sq age_cub if EMPLOYMENT_YR1_v1  == 1  & education==3 //[pw=Sample_Weight]
eststo
	esttab using "${out_tables}/H_income_corr.tex", varwidth(25) nogaps   compress label nobaselevels indicate( "Cubic Age = *age* *age_sq* *age_cub* " ) replace  ///
	nodepvars nonumbers noconstant mtitles("HS or Less" "Some College" "College" "HS or Less, FT" "Some College, FT" "College, FT") star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(3) pr2
	eststo clear
}	
	

* Saves data for Table 1 in Paper H Ordered logit
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
drop if year==2013
keep if year>=2006 
keep if INSCOP_Y1==1 & INSCOP_Y2==1

rename AGE_YR1 age

drop if age>=65
replace age =age-24
gen age_sq  =age^2
gen age_cub =age^3
 
* income quintiles
drop inc_quintile_Y1 
xtile inc_quintile_Y1=TOTALINC_Y1 [aw=Sample_Weight], n(5)
label var inc_quintile_Y1 "Income Quintile"
//label define inc_q 1 "1st" 2 "2nd" 3 "3rd" 4 "4th" 5 "5th"
label values inc_quintile_Y1 inc_q

* ESHI=1 if you work and are have private insurance during the year
gen ESHI=0 if INSCOV_Y1==2 | INSCOV_Y1==3 | EMP_Y1==0 
replace ESHI=1 if INSCOV_Y1==1 & EMP_Y1!=0 

gen I_not_treat=0 if Charges_plus_RX>=.5 & Charges_plus_RX!=.
replace I_not_treat=1 if Charges_plus_RX<.5 & type2!=1 & type2!=.


rename H3 H_next
rename H1 H
rename educ_group education
rename dp dp_record
rename du du_record
rename s s_record
rename inc_quintile_Y1 inc_quintile
gen data=0
keep H_next H education dp_record du_record  s_record ESHI   inc_quintile  age age_sq age_cub data
	
save "${data}/Health_regression.dta", replace 	

}

* Table 22 Appendix regression - ordered logit with latent class
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
rename H3 H2
rename H5 H3

rename du du1
rename dp dp1
rename s s1

drop AGE2 AGE3 AGE4 AGE5

gen AGE2=AGE1+1
gen AGE3=AGE1+2

keep ID H1 H2 H3 s1 s2 du1 du2 dp1 dp2 AGE1 AGE2 AGE3 educ_group year 

drop if H1==. | H2==. | H3==. // making sure everyone has 3 observations
drop if du1==. | dp1==. | s1==. 
drop if AGE3>=65 | AGE1<25

reshape long H s dp du AGE, i(ID educ_group year)

xtset ID AGE
sort ID AGE

constraint 1 [H]1L.H#1bn.Class - [H]1L.H#2.Class = 0
constraint 2 [H]2L.H#1bn.Class - [H]2L.H#2.Class = 0
constraint 3 [H]1bn.Class#c.AGE - [H]2.Class#c.AGE = 0
constraint 4 [H]2.educ_group#1bn.Class - [H]2.educ_group#2.Class = 0
constraint 5 [H]3.educ_group#1bn.Class - [H]3.educ_group#2.Class = 0
constraint 6 [H]1L.dp#1bn.Class - [H]1L.dp#2.Class = 0
constraint 7 [H]1L.du#1bn.Class - [H]1L.du#2.Class = 0
constraint 8 [/H]1bn.Class#c.cut1 - [/H]2.Class#c.cut1 = 1
constraint 9 [/H]1bn.Class#c.cut2 - [/H]2.Class#c.cut2 = 1

fmm 2, startvalues(randomid) lcprob(i.educ_group) constraint(1-9) difficult: ologit H Lib3.H AGE   Li.dp Li.du if  year>=2007
eststo
	esttab using "${out_tables}/Tim_logit1.tex", varwidth(25) nogaps   compress label nobaselevels replace  ///
	nodepvars nonumbers eqlabels(" " ) mtitles("H"  ) star(* 0.1 ** 0.05 *** 0.01) ///
	 se   b(3) 
	eststo clear




}

	
	
* Tables 23 and 24
* DIFFERENCE IN H TRANSITIONS BY INSURANCE
* also investigate transitions for those with high R and low R
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"

gen AGE_CUB_YR1 = AGE_YR1^3
drop if year==2013
gen AGE_CUB_YR2 = AGE_YR2^3

keep if INSCOP_Y2==1 & INSCOP_Y1==1 // this covers R3-R5 which we are using here.

* STAY IN GOOD H 
gen H_trans3=0 if H1==3
replace H_trans3=1 if  (H1==3 & H3==3) 

drop if H1==. | H3==.
keep if AGE_YR1>=24 & AGE_YR1<65


logit H_trans3 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1  i.INSCOV_Y1_alt i.educ_group if ( INSCOV_Y1_alt!=. & INSCOV_Y1_alt!=2 )  //& INSCOV_Y1!=2
eststo margin: margins i.INSCOV_Y1_alt, at(AGE_YR1=45  AGE_SQ_YR1=2025 AGE_CUB_YR1=91125   ) atmeans post
est sto h1
logit H_trans3 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1  i.INSCOV_Y1_alt i.educ_group if ( INSCOV_Y1_alt!=. & INSCOV_Y1_alt!=2 )   //& INSCOV_Y1!=2
eststo margin: margins i.INSCOV_Y1_alt, at(AGE_YR1=55  AGE_SQ_YR1=3025 AGE_CUB_YR1=166375  ) atmeans post
est sto h2
	esttab h1 h2  using "${out_tables}/H_trans_margins_use.tex", varwidth(25) nogaps   compress  label replace  ///
	 nonumbers mtitles("Age 45" "Age 55" ) star(* 0.1 ** 0.05 *** 0.01)  ///
	se   b(3) 
	eststo clear
	


* Repeat the same thing,  by R
label define ins_label 1 "ESHI" 2 "Public" 3 "Uninsured"
label values INSCOV_Y1_alt ins_label


* table 24 left panel
logit H_trans3 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1  i.INSCOV_Y1_alt i.educ_group if R3==1 & ( INSCOV_Y1_alt!=. & INSCOV_Y1_alt!=2)  
eststo margin: margins i.INSCOV_Y1_alt, at(AGE_YR1=45  AGE_SQ_YR1=2025 AGE_CUB_YR1=91125   ) atmeans post
est sto h1
logit H_trans3 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1  i.INSCOV_Y1_alt i.educ_group if R3==1 & ( INSCOV_Y1_alt!=. & INSCOV_Y1_alt!=2)  
eststo margin: margins i.INSCOV_Y1_alt, at(AGE_YR1=55  AGE_SQ_YR1=3025 AGE_CUB_YR1=166375  ) atmeans post
est sto h2
	esttab h1 h2  using "${out_tables}/H_trans_margins_use_R_high.tex", varwidth(25) nogaps   compress  label replace  ///
	 nonumbers nostar eqlabel(High R) mtitles("Age 45" "Age 55" )  ///
	se   b(3) 
	eststo clear
	
* table 24 right panel	
	logit H_trans3 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1  i.INSCOV_Y1_alt i.educ_group if R3==0 & ( INSCOV_Y1_alt!=. & INSCOV_Y1_alt!=2)  
eststo margin: margins i.INSCOV_Y1_alt, at(AGE_YR1=45  AGE_SQ_YR1=2025 AGE_CUB_YR1=91125   ) atmeans post
est sto h1
logit H_trans3 AGE_YR1 AGE_SQ_YR1 AGE_CUB_YR1  i.INSCOV_Y1_alt i.educ_group if R3==0 & ( INSCOV_Y1_alt!=. & INSCOV_Y1_alt!=2)  
eststo margin: margins i.INSCOV_Y1_alt, at(AGE_YR1=55  AGE_SQ_YR1=3025 AGE_CUB_YR1=166375  ) atmeans post
est sto h2
	esttab h1 h2  using "${out_tables}/H_trans_margins_use_R_low.tex", varwidth(25) nogaps   compress  label replace  ///
	 nonumbers eqlabel(Low R) mtitles("Age 45" "Age 55" ) nostar  ///
	se   b(3) 
	eststo clear
	
	}
	

* Saves data for Figure 7
* H by age and education - for plot against model data
qui{	
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if INSCOP_Y1==1
rename AGE_YR1 age

* age groups
gen age_group=27  if age>=25 & age<30
replace age_group=32  if age>=30 & age<35
replace age_group=37  if age>=35 & age<40
replace age_group=42  if age>=40 & age<45
replace age_group=47  if age>=45 & age<50
replace age_group=52  if age>=50 & age<55
replace age_group=57  if age>=55 & age<60
replace age_group=62  if age>=60 & age<65
replace age_group=67  if age>=65 & age<70
replace age_group=72  if age>=70 & age<75
replace age_group=77  if age>=75 & age<80
replace age_group=82  if age>=80 & age<85
replace age_group=87  if age>=85 & age<90
replace age_group=92  if age>=90 & age<95
replace age_group=97  if age>=95 & age<101
label var age_group "Age Group"

gen H_1=0 
replace H_1=1 if H1==1
gen H_2=0 
replace H_2=1 if H1==2
gen H_3=0 
replace H_3=1 if H1==3

preserve
collapse H_1 H_2 H_3, by(age_group)
drop if age_group==.
sort age_group
save "${data}/Health Moments 1 data.dta", nolabel  replace
restore


* H BY EDUC
collapse H_1 H_2 H_3, by(age_group educ_group)
rename educ_group education
sort age_group
reshape wide H_1 H_2 H_3, i(age_group) j(education)
sort age_group
merge age_group using "${data}/Health Moments 1 data.dta"
drop _merge
sort age_group
save "${data}/Health Moments 1 data.dta",  replace



}


* shocks, R and H by age and education - for plot against model data
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if INSCOP_Y1==1
rename AGE_YR1 age
gen H_1=0 
replace H_1=1 if H1==1
gen H_2=0 
replace H_2=1 if H1==2
gen H_3=0 
replace H_3=1 if H1==3

* age groups
gen age_group=27  if age>=25 & age<30
replace age_group=32  if age>=30 & age<35
replace age_group=37  if age>=35 & age<40
replace age_group=42  if age>=40 & age<45
replace age_group=47  if age>=45 & age<50
replace age_group=52  if age>=50 & age<55
replace age_group=57  if age>=55 & age<60
replace age_group=62  if age>=60 & age<65
replace age_group=67  if age>=65 & age<70
replace age_group=72  if age>=70 & age<75
replace age_group=77  if age>=75 & age<80
replace age_group=82  if age>=80 & age<85
replace age_group=87  if age>=85 & age<90
replace age_group=92  if age>=90 & age<95
replace age_group=97  if age>=95 & age<101
label var age_group "Age Group"

replace R3=. if  year<=2006
replace dp=. if  year<=2006
replace du=. if  year<=2006
replace s=. if  year<=2006

collapse R3 dp du s H_1 H_2 H_3, by(age educ_group)
rename educ_group education
sort age
rename R3 R_data
rename dp dp_data
rename du du_data
rename s s_data
rename H_1 H_1_data
rename H_2 H_2_data
rename H_3 H_3_data
replace dp_data=dp_data*100
replace R_data=R_data*100
replace H_1_data=H_1_data*100
replace H_2_data=H_2_data*100
replace H_3_data=H_3_data*100
replace du_data=du_data*100
replace s_data=s_data*100
reshape wide dp_data du_data s_data R_data H_1_data H_2_data H_3_data, i(age) j(education)
rename age age_group
sort age_group
save "${data}/Health Moments 2 data.dta",  replace
}


* Data for Figures 14, 15, 16
* H by age, education and ESHI 
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if INSCOP_Y1==1
rename AGE_YR1 age

gen H_1=0 
replace H_1=1 if H1==1
gen H_2=0 
replace H_2=1 if H1==2
gen H_3=0 
replace H_3=1 if H1==3

keep if age<65 

rename PRIVATE_HI_Y1 ESHI

drop if ESHI==.
collapse H_1 H_2 H_3, by(age educ_group ESHI)
rename educ_group education
rename H_1 H_1_data
rename H_2 H_2_data
rename H_3 H_3_data
sort age
reshape wide H_1_data H_2_data H_3_data, i(age ESHI) j(education)
reshape wide H*, i(age) j(ESHI)
sort age
save "${data}/Health Moments 3 data.dta",  replace
}

* H by age, education and ESHI, FT workers only
* we keep only FT because PT variable not very reliable in MEPS 
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if INSCOP_Y1==1
rename AGE_YR1 age

gen H_1=0 
replace H_1=1 if H1==1
gen H_2=0 
replace H_2=1 if H1==2
gen H_3=0 
replace H_3=1 if H1==3

keep if age<65 & EMPLOYMENT_YR1_v1 == 1 //FT in year 1

gen ESHI=1 if EMPLOYMENT_YR1_v2 == 1
replace ESHI=0 if EMPLOYMENT_YR1_v2 == 0

drop if ESHI==.
collapse H_1 H_2 H_3, by(age educ_group ESHI)
rename educ_group education
rename H_1 H_1_data
rename H_2 H_2_data
rename H_3 H_3_data
sort age
reshape wide H_1_data H_2_data H_3_data, i(age ESHI) j(education)
reshape wide H*, i(age) j(ESHI)
sort age
save "${data}/Health Moments 5 data.dta",  replace
}


* H by employment
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if INSCOP_Y1==1
rename AGE_YR1 age

gen H_1=0 
replace H_1=1 if H1==1
gen H_2=0 
replace H_2=1 if H1==2
gen H_3=0 
replace H_3=1 if H1==3

keep if age<65 

gen employed_yn=0 if EMP_Y1==0
replace employed_yn=1 if EMP_Y1==1 | EMP_Y1==2

collapse H_1 H_2 H_3, by(age educ_group employed_yn)
rename educ_group education
rename H_1 H_1_data
rename H_2 H_2_data
rename H_3 H_3_data
sort age
reshape wide H_1_data H_2_data H_3_data, i(age employed_yn) j(education)
drop if employed_yn==.
reshape wide H*, i(age) j(employed_yn)
sort age
save "${data}/Health Moments 6 data.dta",  replace
}



* Figure 4 - save data to plot against model for R by age
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if  AGE_YR2<65 //  
drop if R5==.
drop if AGE_YR2==.
keep if INSCOP_Y2==1 // this covers R3-R5 which we are using here.
keep if year>=2007  // because we're using year 2 of the survey, it means years 2007 onwards
drop if INSCOV_Y1==1 & labor_force_Y2==1
collapse (mean) R3 R5, by(AGE_YR2)
rename  AGE_YR2 age
sort age
save "${data}/R_ages_ALL.dta",  replace

* as above but keep older ages too
clear
use "${data}/MEPS_merged_Health_Whites.dta"
drop if R5==.
drop if AGE_YR2==.
keep if INSCOP_Y2==1 // this covers R3-R5 which we are using here.
keep if year>=2007  // because we're using year 2 of the survey, it means years 2007 onwards

drop if INSCOV_Y1==1 & labor_force_Y2==1 & AGE_YR2<65
collapse (mean) R3 R5, by(AGE_YR2)
rename  AGE_YR2 age
sort age
save "${data}/R_ages_ALL2.dta",  replace

}


* Table 15, 17 - R regressions
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"

tab R3 if AGE_YR1<65
tab R3 if AGE_YR1<65 & year>=2007 & (INSCOV_Y1==1 | INSCOV_Y1==2)
tab R3 if AGE_YR1<65 & year>=2007 

keep if  AGE_YR2<73 // 
drop if H1==. 
drop if R3==.
drop if AGE_YR2==.
gen AGE_CUB_YR2 = AGE_YR2^3
drop if year==2013
drop if INSCOV_Y1==. | INSCOV_Y2==. 

keep if INSCOP_Y2==1 // this covers R3-R5 which we are using here.

keep if year>=2007  // so because we're using year 2 of the survey, it means years 2007 onwards

label var du2 "du"
label var dp2 "dp"
label var R5 "Health Risk"

* insurance groups that are for the 2 years - we don't want to include peole who transition from one insurance status to another 
* and R3 happens in yr 1 and yr2. 
gen Ins_1_2 = 1 if INSCOV_Y1==1 & INSCOV_Y2==1
replace Ins_1_2 = 2 if INSCOV_Y1==2 & INSCOV_Y2==2
replace Ins_1_2 = 3 if INSCOV_Y1==3 & INSCOV_Y2==3

label define lab_ins_1_2 1 "Private" 2 "Public" 3 "Uninsured"
label values Ins_1_2 lab_ins_1_2

keep if AGE_YR2<65


* Table 15
logit R3 i.PRIVATE_HI_Y1 AGE_YR1 AGE1_sq AGE1_cub  i.H3     i.educ_group if PRIVATE_HI_Y1!=. 
eststo
	esttab using "${out_tables}/Reg_R_bias.tex", varwidth(25) nogaps   compress label nobaselevels indicate( "Cubic Age = *AGE_YR1* *AGE1_sq* *AGE1_cub*" ) replace  ///
	nodepvars nonumbers  noconstant mtitles("High R"  ) star(* 0.1 ** 0.05 *** 0.01)  eqlabel("") ///
	se   b(3) 
	eststo clear

* Table 17 left panel 	
* PROBABILITY OF R (NOT TRANSITION) CONDITIONAL ON INSURANCE
logit R5 AGE_YR2 AGE_SQ_YR2 AGE_CUB_YR2  i.H3   i.INSCOV_Y2  if INSCOV_Y2!=. & AGE_YR2<65
eststo margin: margins i.INSCOV_Y2, at(AGE_YR2=45  AGE_SQ_YR2=2025 AGE_CUB_YR2=91125  ) atmeans post
est sto h1
logit R5 AGE_YR2 AGE_SQ_YR2 AGE_CUB_YR2  i.H3   i.INSCOV_Y2  if INSCOV_Y2!=. & AGE_YR2<65
eststo margin: margins i.INSCOV_Y2, at(AGE_YR2=60  AGE_SQ_YR2=3600 AGE_CUB_YR2=216000  ) atmeans post
est sto h2
	esttab h1 h2 using "${out_tables}/R_logit_margins_ins.tex", varwidth(25) nogaps   compress label replace  ///
	 nonumbers mtitles("High R, age 45"  "High R, age 60" ) star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(3) 
	eststo clear
}



* Table 16 - Logit high R. 
* Figure 3 - R transitions
* Export R transitions to folder with model parameters
qui{
* we exclude anyone who was uninsured in year 1 or year 2. 
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if AGE_YR1<65
drop if R3==. | R5==.
drop if AGE_YR1==.
gen AGE_CUB_YR1 = AGE_YR1^3
drop if year==2013
drop if AGE_YR1<25

keep if INSCOP_Y2==1 // this covers R3-R5 which we are using here.
drop if INSCOV_Y1==. | INSCOV_Y2==. 
keep if year>=2007

//keep if  (AGE_YR2<65 & Ins_1_2==1  ) | AGE_YR2>=65 
keep if ( INSCOV_Y1!=3   & INSCOV_Y2!=3) //| AGE_YR2>=65 


preserve
logit R5 AGE_YR1 AGE_SQ_YR1   i.H3   if   INSCOV_Y1!=3 & INSCOV_Y2!=3 &  R3==0 &  AGE_YR1>=40
eststo
logit R5 AGE_YR1 AGE_SQ_YR1   i.H3   if   INSCOV_Y1!=3 & INSCOV_Y2!=3 &  R3==1 &  AGE_YR1>=40
eststo
	esttab using "${out_tables}/Reg_R.tex", varwidth(25) nogaps   compress label nobaselevels indicate( "Quadratic Age = *AGE_YR1* *AGE_SQ_YR1* " ) replace  ///
	nodepvars nonumbers noconstant eqlabel(none) mtitles("(Low Lagged R)" "(High Lagged R)" ) star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(3) 
	eststo clear
	
	


* filling it to age 100
	        local new = _N + 36 //28
        set obs `new'
			replace AGE_YR1=65 if AGE_YR1==. & AGE_YR1[_n-1]!=.
						replace AGE_YR1=AGE_YR1[_n-1]+1 if  AGE_SQ_YR1==. & AGE_SQ_YR1[_n-1]==.
    * filling in everything else
	fillin AGE_YR1 H3  R3 
		drop if AGE_YR1==. | H3==. | R3==.

	replace AGE_SQ_YR1=AGE_YR1^2 
	replace AGE_CUB_YR1=AGE_YR1^3 
	
	
logit R5 AGE_YR1 AGE_SQ_YR1  i.H3  if R3==0 & AGE_YR1>40 //AGE_CUB_YR1
predict Y  if R3==0

logit R5 AGE_YR1 AGE_SQ_YR1   i.H3   if R3==1  & AGE_YR1>40  //AGE_CUB_YR1 
predict Y1  if R3==1 
replace Y=Y1 if Y1!=.

rename AGE_YR1 age
collapse (mean) Y , by(age H3 R3)

sort R3 H3 age


* Figure 3 left - Prob of transition from low to high R
			#delimit ;
line Y age if  H3==1 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line Y age if H3==2 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line Y age if  H3==3 & R3==0, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
 ||, 
  ylabel(0.0(.20)1)
  xlabel(25(5)100)
  ytitle("Probability")
xtitle("Age")
   title("Probability of Transitioning from Low to High R, by H", color(black))
  legend(label(1 "H=Poor") label(2 "H=Fair")  label(3 "H=Good")   )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\R_age_trans1.eps", replace

* Figure 3 right - Prob of transition from high to high R
			#delimit ;
line Y age if  H3==1 & R3==1, yaxis(1) xaxis(1) lwidth(medthick) lcolor(cranberry) 
|| line Y age if H3==2 & R3==1, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) lpattern(dash)
|| line Y age if  H3==3 & R3==1, yaxis(1) xaxis(1) lwidth(medthick) lcolor(navy) 
 ||, 
  ylabel(0.0(.20)1)
  xlabel(25(5)100)
  ytitle("Probability")
xtitle("Age")
   title("Probability of Transitioning from High to High R, by H", color(black))
  legend(label(1 "H=Poor") label(2 "H=Fair")  label(3 "H=Good")   )
  legend(col(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr	
graph export "${out_figures}\R_age_trans2.eps", replace

* export to folder with model parameters
sort H3 R3 age
drop H3 R3 age
outsheet using "${data_model}/parameters_health_R.txt", nolabel nonames replace
}


* initial H and R distribution by educ at age 25
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if INSCOP_Y1==1 
keep if AGE1<27 & AGE1>23 // keep ages 24, 25 and 26

tabout educ_group H1 using "${out_tables}/H_25.tex", replace ///
    style(tex)   f(1)  clab(%)  cells( row)
	
		gen H_1=0 if H1!=.
		gen H_2=0 if H1!=.
		gen H_3=0 if H1!=.
		replace H_1=1 if H1==1 //poor
		replace H_2=1 if H1==2
		replace H_3=1 if H1==3 //good
				
collapse (mean)  H_1 H_2 H_3 [aw=Sample_Weight], by(educ_group) fast

reshape long H_ , i(educ_group) j(type)
rename H_ H 
sort educ_group type
drop educ_group type
outsheet using "${data_model}/parameters_H25.txt", nolabel nonames replace

clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if INSCOP_Y1==1 
keep if AGE1==25 
keep if INSCOV_Y1==1 | INSCOV_Y1==2
keep if year>=2007
tabstat R3 
tabstat R3 [aw=Sample_Weight]	
bysort educ_group:	tabstat R3 	
collapse (mean)  R3 ,  fast

outsheet using "${data_model}/parameters_R25.txt", nolabel nonames replace
}